<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div slot="header" class="clearfix">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>

                <!-- 搜索表单 -->
                <el-form :model="query" ref="searchquery" label-width="100px">
                    <el-row style="margin-top:20px">
                        <el-col :sm="20" :md="5">
                            <el-form-item label="客户名称:" prop="linkClientName">
                                <div @click="showCustomerDialog('客户')">
                                    <el-input
                                        placeholder="请输入"
                                        v-model="query.linkClientName"
                                        size="small"
                                        readonly
                                    >
                                        <i slot="suffix" class="el-icon-search"></i>
                                    </el-input>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="合同名字:" prop="clientContractName">
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="query.clientContractName"
                                    size="small"
                                    class="handle-input mr10"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="10">
                            <el-form-item label="签订时间:" prop="departNo">
                                <el-form-item prop="createStartTime">
                                    <el-date-picker
                                        v-model="onbordingTime"
                                        type="daterange"
                                        range-separator="至"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        style="width:99%"
                                        @change="onBordingChange"
                                    ></el-date-picker>
                                </el-form-item>
                            </el-form-item>
                        </el-col>
                        

                       
                    </el-row>
                    <el-row>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="负责人:" prop="dutyUserName">
                                <div @click="showTreeDialog('负责人')">
                                    <el-input
                                        placeholder="请输入内容"
                                        v-model="query.dutyUserName"
                                        size="small"
                                        class="handle-input mr10"
                                    >
                                        <i slot="suffix" class="el-icon-search"></i>
                                    </el-input>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5" style="margin-left:10px">
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                @click="submitForm('searchquery')"
                            >搜 索</el-button>
                            <el-button type="info" @click="resetForm('searchquery')">重 置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <!-- 表格 -->
            <table-List
                :tableData="tableData"
                :tableHeader="tableHeader"
                :loading="loading"
                :pagination="pagination"
                @pageChange="pageChange"
                @selectFun="selectFun"
                @sortChange="sortChange"
            ></table-List>
            <!-- 查询 -->
            <tree-search
                :isShow="showSearch"
                :dialogTitle="searchDialogTit"
                @edit="searchBack"
                @close="closeDialog"
            ></tree-search>
            <customer-search
                :isShow="showCustomerSearch"
                :dialogTitle="customerSearchDialogTit"
                method="clientInfoFindAllPageByDTO"
                @edit="customerSearchBack"
                @close="closeDialog"
            ></customer-search>
        </div>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                clientContractName: '', // 合同名称	
                dutyUserId: '', // 责任人id	
                dutyUserName: '', // 责任人	
                linkClientId: '', // 关联的往来单位客户id	
                linkClientName: '', // 关联的往来单位客户名字	
                signEndTime: '', // 订查询结束时间	
                signStartTime: '', // 签订查询开始时间	
                sortColumn: '', // 排序字段 倒序追加 columnName desc
            },
            onbordingTime:'',

            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 60
            },
            tableHeader: [
                // 表头数据
                { prop: 'clientContractNo', label: '合同编号', minWidth: '100px', sortable: 'custom' },
                { prop: 'clientContractName', label: '合同名称', router: 'customerContractDetail', 
                    routerId: 'id',  minWidth: '100px', sortable: 'custom' },
                { 
                    prop: 'linkClientName', 
                    label: '客户', 
                    minWidth: '100px', 
                    router: 'customerInfoDetail', 
                    routerId: 'id', 
                    sortable: 'custom' 
                },
                { prop: 'dutyUserName', label: '责任人', minWidth: '100px', sortable: 'custom' },
                { prop: 'clientContractMoney', label: '合同金额', sortable: 'custom' },
                { prop: 'signTime', label: '签订日期', minWidth: '100px', formatData: function(val) {
                    let date = new Date(val);
                    return appMain.formatDate(date, 'yyyy-MM-dd');
                }, sortable: 'custom' },
                
            ],
            tableData: [],
            multipleSelection: [],
            searchDialogTit: '',
            showSearch: false,
            customerSearchDialogTit: '',
            showCustomerSearch: false,

            //下拉
            clientTypeList: [],
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        // 选择姓名
        showTreeDialog(tit) {
            this.searchDialogTit = tit;
            this.showSearch = true;
        },
        //树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.query.dutyUserName = data.backData.name;
            this.query.dutyUserId = data.backData.id;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        // 选择客户
        showCustomerDialog(tit) {
            this.customerSearchDialogTit = tit;
            this.showCustomerSearch = true;
        },
        // 客户搜索结果返回
        customerSearchBack(data) {
            console.log(data.backData, 'customerSearchBack');
            this.query.linkClientName = data.backData.name;
            this.query.linkClientId = data.backData.id;

            this.showCustomerSearch = false;
        },
        closeDialog (data) {
            this.showSearch = data.showDialog;
            this.showCustomerSearch = false
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            console.log('goEdit', this.multipleSelection);
            this.$router.push({
                path: '/customerContract',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        goAdd() {
            this.$router.push({
                path: '/customerContract'
            });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch(() => {});
        },
        deleteById(id) {
            appMain
                .clientContractInfoDel({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.findAllPage();
        },
        /**
         * 下拉框数据start
         */
        dictItemForPerfection () {
            let dictTypes =['关怀类型'];
            dictTypes.map( item => {
                appMain.dictItem({ typeName: item }).then(res => {
                    if (res.data.code === 20000) {
                        if (item == '客户类型') {
                            this.clientTypeList = res.data.data;
                        }
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            })
        },
        //下拉框选取值
        handleValue(e, dict) {
            console.log(e, '下拉框选取值');
            if (dict == '关怀类型') {
                let item = this.selectChooseItem(this.clientTypeList, e);
                this.query.clientTypeId = item.id;
            } 
        },
        //通用字典选择
        selectChooseItem(item, id) {
            for (const key in item) {
                if (item[key].id == id) {
                    return item[key];
                }
            }
        },
        /**
         * 下拉框数据 end
         */
         //入职时间
        onBordingChange(val) {
            console.log('onBordingChange:', val);
            if (val === null) {
                this.onbordingTime = val;
                this.query.signStartTime= '';
                this.query.signEndTime= ''; 
            } else {
                this.onbordingTime = val;
                this.query.signStartTime = val[0];
                this.query.signEndTime = val[1];
            }

        },
        //查询
        findAllPage() {
            this.loading = true;
            let params = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };
            appMain
                .clientContractInfoFindAllPageByDTO(params)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            this.loading = false;
                        }, 1000);
                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.$message.error(res.data.message);
                        this.loading = false;
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //分页导航
        pageChange(data) {
            this.pagination = data.backData;
            console.log('pageChange:', this.pagination);
            //分页变化--请求接口
            this.findAllPage();
        },
        
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.findAllPage();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 获取区域列表
        regionInfoFindAllPageByName () {
            let sendData= {
                pageNo: 1,
                pageSize: 100,
                record: '',
            }
            appMain
                .regionInfoFindAllPageByName(sendData)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.regionInfoList = res.data.data.list;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
    },
    created() {
        this.findAllPage();
        this.dictItemForPerfection();
        // this.regionInfoFindAllPageByName();
    }
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}
</style>